﻿#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

#region

using System;
using System.Collections.Generic;
using System.IO;
using AzureMRCore;
using AzureMRCore.DataTypes;
using AzureMRCore.MapRed;
using SeqAligner;

#endregion

namespace SWGSample
{
    internal class SWGMapper : Mapper<IntKey, StringValue, IntKey, FileValue, NullKey, NullValue>
    {
        protected override int Map(IntKey key, StringValue value, List<KeyValuePair<NullKey, NullValue>> dynamicData, IOutputCollector<IntKey, FileValue> outputCollector, string programArgs)
        {
            string inputFile = value.GetTextValue();
            //Console.Out.WriteLine(inputFile + "   " + programArgs);
            string[] args = inputFile.Split('$');
            int startX = Convert.ToInt32(args[0]);
            int endX = Convert.ToInt32(args[1]);
            int startY = Convert.ToInt32(args[2]);
            int endY = Convert.ToInt32(args[3]);
            string file = args[4];
            int blockSize = Convert.ToInt32(args[5]);

            string outFile1 = file + startX + "_" + startY;
            string outFile2 = file + startY + "_" + startX;
            Aligner.align(startX, endX, startY, endY, file, outFile1, outFile2, true);
            // Aligner.align(startX % 6400, endX % 6400, startY % 6200, endY % 6200, file, outFile1, outFile2, true);

            //string outFile3 = file + startX + "_" + startY + "2";
            //string outFile4 = file + startY + "_" + startX + "3";
            //Aligner.align(startX, endX, startY, endY, file, outFile1, outFile2, false);

            outputCollector.Collect(IntKey.GetInstance(startX/blockSize), FileValue.GetInstance(new FileInfo(outFile1)));
            if (startX != startY)
            {
                outputCollector.Collect(IntKey.GetInstance(startY/blockSize),
                                        FileValue.GetInstance(new FileInfo(outFile2)));
            }
            return 0;
        }
    }
}